// import {
// 	get,
// 	post
// } from '../request/http.js'
import store from "../store/index"
const BASE_URL = "http://192.168.1.129:8899"

const get = (url) => {
	return http({
		url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET'
	})
}

const post = (url, data) => {
	return http({
		url, data
	})
}

const http = (options) => {

	let token = store.state.token;
	uni.showLoading({
		title: '加载中'
	});
	let config = {
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/json', //自定义请求头信息
		},
		method: 'post',
		...options

	}
	config.url = BASE_URL + config.url;
	if (token) {
		config.header.Token = token;
	}
	return uni.request(config).then(([errMsg, response]) => {
		if (errMsg) {
			return Promose.reject(errMsg);
		}
		let {
			code,
			desc,
			data
		} = response.data
		// 后台
		if (code == 1004 || code == 1005) {
			// 清理过期token
			store.commit("logout");
			// 判断当前是否已经跳转登录页面
			let pages = getCurrentPages();
			let page = pages[pages.length - 1];
			if (page.route != 'pages/login/login') {
				uni.navigateTo({
					url: '/pages/login/login?returnUrl=/' + page.route
				})
			}
			return Promose.reject(response);
		}
		return response.data;
	}).catch((error) => {
		if (error && error.response) {
			return Promose.reject(error.response);
		}
	})
}
//demo
const getPage = p => post('api/admin/neigh/getPage', p);
const useLogin = use => post('/api/user/account/login',use)
const userRegister = useMesg => post('/api/bmUser/register',useMesg)
const noteLogin = phone => post('/api/user/account/sendVerificationCode',phone)
const forgetPwd = userPhone => post('/api/bmUser/forgetPwd',userPhone)
export {
	getPage,
	useLogin,
	userRegister,
	noteLogin,
	forgetPwd
}
